﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data.SqlClient;
using System.Data;


namespace WorkDAL.Users
{
    public class UserDataAccess
    {
        WorkerDataContext workContext = new WorkerDataContext();

        public static UserDataAccess Instance()
        {
            return new UserDataAccess();
        }

        public List<tbUser> UserGetList()
        {
            var userList = from c in this.workContext.tbUsers
                               select c;

            return userList.ToList();
        }

        public List<tbUser> UserGetList(string branchCode)
        {
            var userList = from c in this.workContext.tbUsers
                           where c.BranchCode == branchCode
                           select c;

            return userList.ToList();
        }

        public tbUser UserGet(string username)
        {
            var user = from c in this.workContext.tbUsers
                       where c.UserName == username
                       select c;

            return user.FirstOrDefault();
        }

        public void CreateUser(tbUser user)
        {
            workContext.tbUsers.InsertOnSubmit(user);
            workContext.SubmitChanges();
        }

        public void UpdateUser(tbUser item)
        {
            tbUser user = workContext.tbUsers.Single(p => p.UserName == item.UserName);
            user.UserFullName = item.UserFullName;
            user.Password = item.Password;
            user.BranchCode = item.BranchCode;

            workContext.SubmitChanges();
        }

        public void UpdatePermission(tbUser item)
        {
            tbUser user = workContext.tbUsers.Single(p => p.UserName == item.UserName);
            user.Permisstion = string.IsNullOrEmpty(item.Permisstion)? string.Empty: item.Permisstion;

            workContext.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<UsersPermissionObject> GetMenuPermission(string userName)
        {
            List<UsersPermissionObject> menuPermission = new List<UsersPermissionObject>();
            using (SqlConnection sqlConnection = new SqlConnection(CommonSettings.ConnectionString))
            {
                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand("MenuGetListPermission", sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userName;

                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (sqlDataReader.Read())
                    {
                        if (sqlDataReader.HasRows)
                        {
                            UsersPermissionObject item = new UsersPermissionObject();

                            if (sqlDataReader["Id"] != DBNull.Value)
                            {
                                item.Id = (int)sqlDataReader["Id"];
                            }

                            if (sqlDataReader["MenuLevel"] != DBNull.Value)
                            {
                                item.MenuLevel = (int)sqlDataReader["MenuLevel"];
                            }

                            
                            item.Group = sqlDataReader["Group"].ToString();
                            item.MenuName = sqlDataReader["MenuName"].ToString();
                            item.Caption = sqlDataReader["Caption"].ToString();
                            item.Click = sqlDataReader["Click"].ToString();

                            if (sqlDataReader["AllowPermission"] != DBNull.Value)
                            {
                                item.AllowPermission = (bool)sqlDataReader["AllowPermission"];
                            }

                            menuPermission.Add(item);
                        }
                    }
                }


            }
            return menuPermission;
        }


    }
}
